package com.young.nyd.web;

import com.young.cms.model.AdministrativeRegion;
import com.young.cms.service.IAdministrativeRegionService;
import com.young.common.core.excel.ExcelExport;
import com.young.common.core.excel.ExcelImport;
import com.young.common.core.excel.IExcelExport;
import com.young.common.core.excel.IExcelImport;
import com.young.common.util.CommonUtil;
import com.young.common.util.Result;
import com.young.common.util.StringUtils;
import com.young.common.util.Page;
import com.young.interfaces.log.annotation.Log;
import com.young.nyd.model.UviSupervision;
import com.young.nyd.service.IUviSupervisionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.young.common.util.Result.buildFail;

/**
 * 监督管理表服务
 * Created by hu on 2018-11-7.
 */
@Controller
@RequestMapping("/nyd/uviSupervision")
public class UviSupervisionController {

    @Resource(name = "uviSupervisionService")
    IUviSupervisionService service;
    @Resource(name = "administrativeRegionService")//行政区划
            IAdministrativeRegionService administrativeRegionService;

    /**
     * 获取数据列表
     *
     * @param obj
     * @param request
     * @return
     */
    @Log("查询监督管理表列表")
    @RequestMapping("/getList")
    @ResponseBody
    public Object getList(UviSupervision obj, HttpServletRequest request) {
        List<UviSupervision> list = service.getList(obj);
        return new Result(list);
    }

    /**
     * 获取分页数据
     *
     * @param obj
     * @param request
     * @return
     */
    @Log("分页查询监督管理表列表")
    @RequestMapping("/getPage")
    @ResponseBody
    public Object getPage(UviSupervision obj, HttpServletRequest request) {
        Page page = CommonUtil.getPageFromRequest(request);
        List<UviSupervision> list = service.getPage(obj, page);
        for (UviSupervision u : list) {
            AdministrativeRegion a = administrativeRegionService.get(u.getRegions());
            u.setRegions(a.getName());
        }
        page.setData(list);
        return page;
    }

    /**
     * 获取单条数据
     *
     * @param id
     * @param request
     * @return
     */
    @Log("查询监督管理表")
    @RequestMapping("/get")
    @ResponseBody
    public Object get(String id, HttpServletRequest request) {
        UviSupervision obj = service.get(id);
        return new Result(obj);
    }

    /**
     * 统计数量
     *
     * @param obj
     * @param request
     * @return
     */
    @Log("统计监督管理表数量")
    @RequestMapping("/count")
    @ResponseBody
    public Object count(UviSupervision obj, HttpServletRequest request) {
        return new Result(service.count(obj));
    }

    /**
     * 新增
     *
     * @param obj
     * @param request
     * @return
     */
    @Log("新增监督管理表")
    @RequestMapping("/insert")
    @ResponseBody
    public Object insert(UviSupervision obj, HttpServletRequest request) throws Exception {
        String province = request.getParameter("province");
        String city = request.getParameter("city");
        String county = request.getParameter("county");
        String regions;
        if (county.equals("")) {
            if (city.equals("")) {
                if (province.equals("")) {
                    return buildFail();
                } else {
                    regions = province;
                }
            } else {
                regions = city;
            }
        } else {
            regions = county;
        }
        obj.setRegions(regions);

        /*//添加序号
        String max = service.maxSerialnumber(obj);
        if (max == null || "".equals(max)) {
            int m = 1;
            max = String.valueOf(m);
            obj.setSerialnumber(max);
        } else {
            int max2 = Integer.valueOf(max) + 1;
            obj.setSerialnumber(String.valueOf(max2));
        }*/
        //obj.setId(CommonUtil.getUUID());
        service.insert(obj);
        return new Result();
    }

    /**
     * 修改
     *
     * @param obj
     * @param request
     * @return
     */
    @Log("修改监督管理表")
    @RequestMapping("/update")
    @ResponseBody
    public Object update(UviSupervision obj, HttpServletRequest request) {
        service.update(obj);
        return new Result();
    }

    /**
     * 删除
     *
     * @param ids
     * @param id
     * @param request
     * @return
     */
    @Log("删除监督管理表")
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String ids, String id, HttpServletRequest request) {
        Result result = new Result();
        if (StringUtils.isNotBlank(ids)) {
            String[] idArr = ids.split(",");
            service.delete(idArr);
        } else if (StringUtils.isNotBlank(id)) {
            service.delete(id);
        } else {
            result.setCode(-1);
            result.setInfo("删除失败:待删除编号无效!");
        }
        return result;
    }

    /**
     * 导入
     *
     * @param file
     * @param request
     * @return
     */
    @Log("导入监督管理表")
    @RequestMapping("/import")
    @ResponseBody
    public Object importExcel(MultipartFile file, HttpServletRequest request) {
        Result result = new Result();
        try {
            if (file != null && !file.isEmpty()) {
                //excel导入处理,返回excel中的数据集合
                IExcelImport ei = new ExcelImport(file);//将文件转为ExcelImport对象
                //从excel读取数据
                List<UviSupervision> list = ei.getImportDataAsBean(UviSupervision.class);
                if (list != null && list.size() > 0) {
                    int num = service.batchInsert(list);//批量插入
                    result.setInfo("成功导入数据" + num + "条!");
                } else {
                    result.setCode(-1);
                    result.setInfo("导入失败:excel解析后结果为空!");
                }
            } else {
                result.setCode(-1);
                result.setInfo("导入失败:文件为空!");
            }
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(-1);
            result.setInfo("导入失败!");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            result.setCode(-1);
            result.setInfo("导入失败!");
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(-1);
            result.setInfo("导入失败!");
        }
        return result;
    }

    /**
     * 导出
     *
     * @param obj      查询参数
     * @param request
     * @param response
     * @throws Exception
     */
    @Log("导出监督管理表")
    @RequestMapping("/export")
    public void exportExcel(UviSupervision obj, HttpServletRequest request, HttpServletResponse response, String ids) throws Exception {
        List<UviSupervision> list = new ArrayList<UviSupervision>();
        if (ids != null && !"".equals(ids)) {
            String[] ids1 = ids.split(",");
            for (String id : ids1) {
                UviSupervision u = service.get(id);
                list.add(u);
            }
        } else {
            list = service.getList(obj);
        }
        if (list != null && list.size() > 0) {
            IExcelExport ee = new ExcelExport();
            ee.insertBeanList(list, UviSupervision.class);
            ee.write2Response(response, "监督管理" + System.currentTimeMillis() + ".xls");
        } else {
            response.getWriter().write("数据为空!");
        }
    }

}